<template>
  <div class="tap">
    <el-card class="box-card" style="margin: 10px 0px">
      <div slot="header" class="clearfix">
        <el-tabs v-model="activeName">
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="viset"></el-tab-pane>
        </el-tabs>
        <div class="botm">
          <span @click="setDay">本日</span>
          <span @click="setWeek">本周</span>
          <span @click="setMonth">本月</span>
          <span @click="setYear">本年</span>
          <el-date-picker
          v-model="date"
            class="date"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </div>
      </div>

      <el-row :gutter="10">
        <el-col :span="20">
          <div class="charts" ref="charts"></div>
        </el-col>
        <el-col :span="4">
          <h3>门店{{title}}排名</h3>
        <ul>
            <li v-for="(lists,index) in list.orderRank" :key="index">
                <span class="rindex">{{lists.no}}</span>
                <span class="main">{{lists.name}}</span>
                <span class="rigth">{{lists.money}}</span>
            </li>
    
        </ul>
            
        </el-col>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import * as echarts from "echarts";
import dayjs from 'dayjs'
import {mapState} from 'vuex'
export default {
  data() {
    return {
      activeName: "sale",
      bar:null,
      date:[]
    };
  },
  mounted() {
    this.bars()
  },
  methods: {
    bars() {
       this.bar = echarts.init(this.$refs.charts);
      this.bar.setOption({
        title:{
            text:'销售额趋势'
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: [],
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: [],
          },
        ],
      });
    },
    setDay() {
      const day = dayjs().format('YYYY-MM-DD')
      this.date = [day,day]
    },
    setWeek() {
      const start = dayjs().day(1).format('YYYY-MM-DD')
      const end = dayjs().day(7).format('YYYY-MM-DD')
      this.date = [start,end]
    },
    setMonth() {
      const start = dayjs().startOf('month').format('YYYY-MM-DD')
      const end = dayjs().endOf('month').format('YYYY-MM-DD')
      this.date = [start,end]
    },
    setYear() {
       const start = dayjs().startOf('year').format('YYYY-MM-DD')
      const end = dayjs().endOf('year').format('YYYY-MM-DD')
      this.date = [start,end]
    }
  },
  computed: {
    title() {
        return this.activeName=='sale'?'销售额':'访问量'
    },
    ...mapState({
      list:(state)=>state.home.data
    })
  },
  watch: {
    title() {
        this.bar.setOption({
            title:{
                text:this.title + '趋势'
            },
            xAxis:{
              data:this.title=='销售额'? this.list.orderFullYearAxis : this.list.userFullYearAxis
            },
             series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: this.title=="销售额"? this.list.orderFullYear : this.list.userFullYear
          },
        ],

        })
    },
    list() {
        this.bar = echarts.init(this.$refs.charts);
      this.bar.setOption({
        title:{
            text:'销售额趋势'
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "shadow",
          },
        },
        grid: {
          left: "3%",
          right: "4%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: [
          {
            type: "category",
            data: this.list.orderFullYearAxis,
            axisTick: {
              alignWithLabel: true,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
          },
        ],
        series: [
          {
            name: "Direct",
            type: "bar",
            barWidth: "60%",
            data: this.list.orderFullYear,
          },
        ],
      });
    }
  }
};
</script>

<style scoped>
.clearfix {
  display: flex;
  position: relative;
  justify-content: space-between;
}
.tap {
  width: 100%;
}
.botm {
  position: absolute;
  right: 10px;
}
.date {
  width: 250px;
  margin: 0px 20px;
}
.botm span {
  margin: 0px 10px;
}
.charts {
  width: 100%;
  height: 300px;
}
ul {
    width: 100%;
    height: 300px;
    list-style: none;
    padding: 0;
}
ul li {
    height: 8%;
    margin: 10px 0px;
}
span {
    margin: 0px 10px;
}
.rindex {
    display: inline-block;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background-color: black;
    color: white;
    text-align: center;
    line-height: 20px;
}
.rigth {
    float: right;
}
ul .main {
  margin: 0;
}
</style>
